<?php

namespace Biz\model\bizAd;

use Biz\pockets\modelTrait\BizModelTrait;
use Doraemon\pockets\datebase\BaseDbModel;

class AdRechargeOrder extends BaseDbModel
{
    const _CONFIG_FILE_ = 'db_biz_ad';

    const _CONFIG_SELECT_ = 'db_biz_ad';

    const _DATABASE_ = 'biz_ad';

    const _TABLE_NAME = 'hhz_ad_recharge_order';

    // 创建订单
    const ORDER_STATUS_CREATE = 1;
    // 完成订单
    const ORDER_STATUS_FINISHED = 2;
    // 取消订单
    const ORDER_STATUS_CANCEL = 3;
    // 系统取消订单
    const ORDER_STATUS_SYS_CANCEL = 4;

    // 未支付
    const PAY_STATUS_UNPAID = 1;
    // 已支付
    const PAY_STATUS_PAID = 2;
    // 支付失败
    const PAY_STATUS_FAIL = 3;

    // 支付平台（1：支付宝；2：微信；3：银行转账）
    const PAY_PLATFORM_ALI_PAY = 1;
    const PAY_PLATFORM_WE_CHAT = 2;
    const PAY_PLATFORM_BANK = 3;

    const PAY_PLATFORM = [
        '' => '全部',
        self::PAY_PLATFORM_ALI_PAY => '支付宝',
        self::PAY_PLATFORM_WE_CHAT => '微信',
        self::PAY_PLATFORM_BANK => '银行转账'
    ];

    use BizModelTrait;

    /**
     * 创建订单
     * @param $orderType
     * @param $dsn
     * @param $uid
     * @param $identify
     * @param $utype
     * @param $pay_platform
     * @param $price
     * @param $remark
     * @param $adminId
     * @return int
     */
    public static function createOrder(
        $dsn,
        $uid,
        $utype,
        $identify,
        $pay_platform,
        $price,
        $remark,
        $adminId
    ) {
        $data = [
            'dsn' => $dsn,
            'uid' => $uid,
            'utype' => $utype,
            'identify' => $identify,
            'pay_platform' => $pay_platform,
            'price' => $price,
            'add_time' => time(),
            'remark' => $remark,
            'admin_id' => $adminId,
        ];

        return self::dataInsert($data);
    }

    /**
     * 查询订单信息
     * @param $order_sn
     * @return array|mixed
     */
    public static function getOrder($order_sn)
    {
        return self::dataSelectInfo(['dsn' => $order_sn]);
    }

    /**
     * 更新支付状态
     * @param $order_sn
     * @param $other_sn
     * @param $pay_status
     * @param $pay_time
     * @param $pay_platform
     * @param $amount
     * @param int $order_status
     * @param $tradeProof
     * @return bool|int
     */
    public static function updatePayStatus(
        $order_sn,
        $other_sn,
        $pay_status,
        $pay_time,
        $pay_platform,
        $amount,
        $order_status,
        $trade_proof = ''
    ) {
        if ($pay_status == self::PAY_STATUS_FAIL || $pay_status == self::PAY_STATUS_PAID) {
            $data = [
                'other_sn' => $other_sn,
                'pay_platform' => $pay_platform,
                'amount' => $amount,
                'pay_time' => $pay_time,
                'pay_status' => $pay_status,
                'order_status' => $order_status,
                'update_time' => time(),
            ];
            if(!empty($trade_proof)){
                $data['trade_proof'] = $trade_proof;
            }
            return self::dataUpdate($data, ['dsn' => $order_sn], true);
        }
        return false;
    }

}